package com.ruoyi.back.service.impl

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.util.StrUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.ruoyi.app.common.*
import com.ruoyi.back.domain.dto.BackUserDto
import com.ruoyi.back.domain.vo.BackUserVo
import com.ruoyi.back.service.BackUserService
import com.ruoyi.common.core.domain.entity.SysUser
import com.ruoyi.common.core.page.TableSupport
import com.ruoyi.common.exception.ServiceException
import com.ruoyi.common.utils.SecurityUtils
import com.ruoyi.system.service.ISysRoleService
import com.ruoyi.system.service.ISysUserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class BackUserServiceImpl : BackUserService {

    @Autowired
    private lateinit var userService: ISysUserService

    override fun saveBackUser(backUserDto: BackUserDto): Data<Boolean> {

        userService.selectUserByUserName(backUserDto.userName)?.run {
            throw ServiceException("登陆名不允许重复!")
        } ?: kotlin.run {
           return backUserDto.userId?.run {
                userService.updateUser(SysUser().apply {
                    roleIds = arrayOf(3)
                    roleId = 3
                    BeanUtil.copyProperties(backUserDto, this)
                })

                Ok("修改用户成功", true)
            } ?: kotlin.run {
                Ok("添加用户成功", userService.insertUser(SysUser().apply {
                    BeanUtil.copyProperties(backUserDto, this)
                    roleIds = arrayOf(3)
                    roleId = 3
                    password = SecurityUtils.encryptPassword(backUserDto.password)
                }) > 0)
            }
        }
    }


    override fun backUserPage(searchParam: String?): PageData<BackUserVo> =
        TableSupport.getPageDomain().let {
            userService.page(
                Page(it.pageNum.toLong(), it.pageSize.toLong()),
                KtQueryWrapper(SysUser::class.java)
                    .eq(SysUser::roleId, 3)
                    .eq(StrUtil.isNotBlank(searchParam), SysUser::nickName, searchParam)
                    .or()
                    .eq(StrUtil.isNotBlank(searchParam), SysUser::userName, searchParam)
            )
                .let { page: Page<SysUser> ->
                    Page<BackUserVo>().apply {
                        BeanUtil.copyProperties(page, this)
                        records = page.records.takeIf { it.isNotEmpty() }
                            ?.run {
                                map {
                                    BackUserVo().apply {
                                        BeanUtil.copyProperties(it, this)
                                        createTime = it.createTime?.format()
                                    }
                                }
                            }
                    }
                }
        }.pageInfo()

    override fun removeUser(userIds: String): Data<Boolean> =
        Ok("批量移除成功", userService.deleteUserByIds(userIds.split(",").map { it.toLong() }.toTypedArray()) > 0)
}